//表单校验
function validForm(e,extraValid) {
	//移除上次校验提示信息 
	var fiter = e.find('.has-error');
	fiter.removeClass('has-error');
	e.find('.has-success').removeClass('has-success');
	e.find('i[data-toggle=tooltip]').tooltip('hide').remove();
	var $controls = e.find('[name]').filter('.vtype');
	var msg;
	var flag = true;
	var currentFlag = true;
	var $field;
	var vtype;
	var val;

	for (var i = 0; i < $controls.length; i++) {
		currentFlag = true;
		$field = $($controls[i]);
		val = $field.val();
		vtype = $field.attr('vtype');
		if(!vtype) {
			continue;
		}
		var str = vtype.split(",");
		var item = {};
        var regex = function (pattern) {
            var re = new RegExp(pattern);
            if (re.test(val)) {
            	return true;
            }
            return false;
        }
		for(var s = 0; s < str.length; s++) {
			var data = str[s].split(":");
			item[data[0]] = data[1];
		}
		if (item) {
			//必填项
			if(item.required && !val) {
				currentFlag = false;
				msg = "不能为空！";
			}
            //字符长度范围
			if (item.rangeLength && currentFlag) {
                var tmp = item.rangeLength.split('-');
                if (val.length < parseInt(tmp[0]) || val.length > parseInt(tmp[1])) {
                    currentFlag = false;
                    msg = "字符长度必须在 " + tmp[0] + "与 " + tmp[1] + " 之间！";
                }
            }
            //整型
			if (item.int && currentFlag && !regex(/^^([+-]?)[0-9]+$/)) {
                currentFlag = false;
                msg = "必须填写数字！";
            }
            //浮点数
			else if (item.float && currentFlag && !regex(/^([+-]?)[0-9]*\.[0-9]+$/)) {
				currentFlag = false;
				msg = "必须填写带小数的数字！";
            }
            //数字
			else if (item.number && currentFlag && (!regex(/^([+-]?)[0-9]*\.[0-9]+$/) || !regex(/^^([+-]?)[0-9]+$/))) {
                currentFlag = false;
                msg = "必须填写数字！";
            }
            //数字范围
			if (item.range && currentFlag) {
                var tmp = item.range.split('-');
                if (parseFloat(val) < parseFloat(tmp[0]) || parseFloat(val) > parseFloat(tmp[1])){
                    currentFlag = false;
                    msg = "数字范围必须在 " + tmp[0] + "与 " + tmp[1] + " 之间！";
                }
            }
			//最大值 
			if (item.maxLength && currentFlag && val.length > parseInt(item.maxLength)) {
		       currentFlag = false;
		       msg = "不能超过 " + item.maxLength + " 个字符！";
			}
			//最小值
			if (item.minLength && currentFlag && val.length < parseInt(item.minLength)) {
            	currentFlag = false;
            	msg = "不能少于 " + item.minLength + " 个字符！";
            }
		}
		
		//以上校验通过时的额外校验
		if(extraValid && currentFlag) {
			var _validResult = extraValid($field);
			if(_validResult) {
				currentFlag = _validResult['currentFlag'];
				msg = _validResult['msg'];
			}
		}

		if (currentFlag) {
			$field.closest('.form-group').addClass('has-success');
			$field.parent().append('<i class="icon icon-check-circle text-success validate-info" data-toggle="tooltip" title="验证通过"></i>');
			continue;
	   	} else {
	   		flag = false;
	   	}

	   	$field.closest('.form-group').addClass('has-error');
	   	var fieldName = $('label[for=' + $field.attr('name') + ']').text();
	   	$field.parent().append('<i class="icon icon-exclamation-sign text-danger validate-info" data-toggle="tooltip" title="' + fieldName + ' ' + msg + '" ></i>');
	}
	if (!eval(flag)) {
		e.find('i[data-toggle=tooltip].text-danger').tooltip({ placement: 'left', template: '<div class="tooltip tooltip-danger"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>' }).tooltip('show');
		e.find('i[data-toggle=tooltip].text-success').tooltip({ placement: 'left', template: '<div class="tooltip tooltip-success"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>' });
	}
	return flag;
}